﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Data.SqlClient;

namespace 餐饮管理系统
{
    public partial class MainWindow : Window
	{
        private void loadBranchSelected()
        {
            ListBoxItem branch = CenterListBox.SelectedItem as ListBoxItem;

            if (branch == null)
                return;

            string branchName = branch.Content.ToString().Trim();
            string sql = "select * from BranchTable where name = '" + branchName + "'";
            SqlDataReader dataReader = databaseConnect.GetDataReader(sql);
            while (dataReader.Read())
            {
                BranchBranchNameTextBox.Text = dataReader[0].ToString().Trim();
                BranchPhoneTextBox.Text = dataReader[1].ToString().Trim();
                BranchAddressTextBox.Text = dataReader[2].ToString().Trim();
                BranchRemarkTextBox.Text = dataReader[3].ToString().Trim();
            }
            dataReader.Close();
        }

        private void BranchInformationSaveButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!HomePhoneRegex.IsMatch(BranchPhoneTextBox.Text))
            {
                MessageBox.Show("电话输入错误！");
                return;
            }

            string sql = "update BranchTable "
                       + "set name = '" + BranchBranchNameTextBox.Text + "',"
                       + "    phone = '" + BranchPhoneTextBox.Text + "',"
                       + "    address = '" + BranchAddressTextBox.Text + "',"
                       + "    remark = '" + BranchRemarkTextBox.Text + "' "
                       + "where name = '" + (CenterListBox.SelectedItem as ListBoxItem).Content.ToString().Trim() + "'";
            databaseConnect.ExecuteQuery(sql);
            loadBranchSelected();
        }
    }
}       